#!/bin/sh


usage() {
    echo "usage: $0 TITLE [url]"
}


if [ $# -lt 1 ]; then
    usage
    exit 1
fi


declare -A userinfo

echo "------"

env


s_project_name="Azal"

s_project_name_l="azal"

s_domain_name="kubesphere.azallivegroup.com"

s_devops_name="${JOB_NAME%%/*}"

s_pipeline_name="${JOB_NAME#*/}"

s_pipeline_name="${s_pipeline_name%%/*}"

s_branch_name="${JOB_NAME##*/}"

s_notify_url="https://${s_domain_name}/${s_project_name_l}/clusters/host/devops/${s_devops_name}/pipelines/${s_pipeline_name}/branch/${s_branch_name}/activity"



userinfo=(
    [zhangzhanwei]=18814184168
    [胡青峰]=15168281463
    [qingfeng]=15168281463
    [zhang]=15652905618
    [张杰]=15652905618
    [阮传丽]=15059293793
    [ruanchuanli]=15059293793
    [lumin]=15868148457
    [Greet_Li]=17789191844
    [wyt]=15833054525)
# echo ${!userinfo[*]}

# echo ${userinfo[*]}


git config --global --add safe.directory "*"

TITLE="$1"

at_phone=""

if [ _"${TITLE}" = _"构建审核" ]; then
    at_phone=" @15833054525"
else
    GIT_AUTHORS=$(git log --pretty=format:"%an" ${GIT_PREVIOUS_COMMIT}..${GIT_COMMIT})
    if [ _"${GIT_AUTHORS}" = _"" ]; then
        # 获取最近一次提交的作者名
        GIT_AUTHORS=$(git log --pretty=format:"%an" -1)
        # %h 提交ID、%an 提交人、%s 提交信息
        GIT_MSG=$(git log --pretty=format:"  \\n  #### **提交ID：** %h  \\n  > **提交人：** %an  \\n  > **提交信息：** %s" -1)
    else
        # ${GIT_PREVIOUS_COMMIT}..${GIT_COMMIT} ：为了精确控制提交历史范围，确保只获取本次构建引入的新提交，而非整个仓库历史
        GIT_MSG=$(git log --pretty=format:"  \\n  #### **提交ID：** %h  \\n  > **提交人：** %an  \\n  > **提交信息：** %s" ${GIT_PREVIOUS_COMMIT}..${GIT_COMMIT})
    fi
    declare -A unique_user_phones  # 使用关联数组来存储唯一的 user_phone
    for author in $GIT_AUTHORS; do
        user_phone=${userinfo[${author}]}
        # 使用关联数组检查 user_phone 是否已经存在
        if [ -z "${unique_user_phones["$user_phone"]}" ]; then
            unique_user_phones["$user_phone"]=1  # 记录已经处理过的 user_phone
        fi
    done
    for phone in "${!unique_user_phones[@]}"; do
        at_phone+=" @$phone"
    done
fi


echo "------"

echo ${GIT_AUTHORS}

echo "------"


if [ _"${at_phone}" = _"" ]; then
    at_phone=" @15652905618"
fi


CONTENT="### ${TITLE}${at_phone}  \\n  ### **项目名：${s_project_name}**  \\n 
#### **应用名：** ${APP_NAME}  \\n  #### **分支：** ${BRANCH_NAME}  \\n  ####
**构建次数：** ${BUILD_NUMBER}${GIT_MSG}  \\n  #### 查看[构建详情](${s_notify_url})"


URL="$2"

if [ _"${URL}" != _"" ]; then
    array=(${URL//:/ })
    if [ _"${array[0]}" = _"https" ]; then
        CONTENT="${CONTENT}  \\n  #### 查看[部署连接](${URL})"
    fi
fi


CHAT_CONTENT_TYPE='Content-Type: application/json'

CHAT_WEBHOOK_URL='https://oapi.dingtalk.com/robot/send?access_token'

CHAT_WEBHOOK_KEY='c9613b0d4f5288e3cc21efad4e35e01e5794d108584f05f13fe1b2e1a4825346'


curl "${CHAT_WEBHOOK_URL}=${CHAT_WEBHOOK_KEY}" \
    -H "${CHAT_CONTENT_TYPE}" \
    -d '
    {
    "msgtype": "markdown",
    "markdown": {
        "title": "'"${TITLE}"'",
        "text": "'"${CONTENT}"'"
    },
    "at": {
            "atMobiles": [
                "18814184168",
                "15168281463",
                "15868148457",
                "15059293793",
                "15652905618",
                "17789191844",
                "15833054525"
            ]
        }
    }'